#!/bin/bash
# >>>>>>>>>>>>>>>>>>>>
#export pj
Date=$(date "+%Y%m%d")
Rom_name=$(cat ./Rom_Name.txt)
 
while true
do
printf "\033c"
read -p "Please enter the project name: " pj
if [  -n "$pj" ];then
   if [ ! -d "$pj" ];then
	mkdir ./$pj
	break
	else
	echo "This Rom project already exists"
	read -p "do you continue to use the project?[y/n] " p
	if [ "$p" == "y" ];then
	break
	else 
	read -p "Press Enter re-enter another project name !"
	fi
   fi
   else
   	read -p "The project name is empty, Press Enter re-enter!"
fi
done

while true
do
trap INT

printf "\033c"
echo -e "\t\t ============================================="
echo -e "\t\t =           Welcome R_Kitchen.MIUI          ="
echo -e "\t\t =                                           ="
echo -e "\t\t = 1.Unzip Rom         2.Unpack dat(br=>dat) =" 
echo -e "\t\t = 3.Repack System.img 4.Generate dat.br     ="
echo -e "\t\t = 5.Boot.img_tool     6.Generate Rom        ="
echo -e "\t\t =                                           ="
echo -e "\t\t =              0.Clean Project              ="
echo -e "\t\t =                Q(q).Exit                  ="
echo -e "\t\t =                                           ="
echo -e "\t\t =   Date: `date`   ="
echo -e "\t\t ============================================="
echo $C
read -p "Please Input Select: " input_var

if [ "$input_var" == 1 ];then 
    ls | grep ".zip$" > ./$pj/Rom_Name.txt
    echo "Unzip, please wait ..."
  #  unzip -O cp936 ./*.zip "*.br" "*.dat" "*.list" -d ./$pj >nul
  #  unzip -O cp936 -q ./*.zip -d ./$pj
	unzip -O cp936 ./*.zip -d ./$pj
    	if [ $? == 0 ];then
		mv -f ./$pj/firmware-update ./$pj/Latest_Rom/
		mv -f ./$pj/META-INF ./$pj/Latest_Rom/
		mv -f ./$pj/dynamic_partitions_op_list ./$pj/Latest_Rom/
		mv -f ./$pj/exaid.img ./$pj/Latest_Rom/
		mv -f ./$pj/boot.img ./AIK-Linux/
		echo 
    	echo "Decompression is complete !"
        read -p "Press any key return Main Menu."
    	else
		echo 
    	echo -e "Failed decompressed! \nPlease check whether the compressed package exists."
        read -p "Press any key return Main Menu."
    	fi
fi

if [ "$input_var" == 2 ];then
	if [ ! -e ./$pj/system.new.dat ];then
      if [ -f ./$pj/system.new.dat.br ];then
	 	read -p "system.new.dat.br exist, Whether to convert ? [y/n]: " var1
	 	if [ "$var1" == "y" ];then
	 	brotli -d ./$pj/system.new.dat.br
	 	echo "system.br==>dat done!"
	 	echo "Unpacking System.img..."
	 	python3 ./tools/sdat2img.py $pj/system.transfer.list $pj/system.new.dat $pj/system_.img
	 	echo "Conversion complete !"
	 	read -p "Press any key continue..."
	 	fi
	  fi
	else
		 echo "system.new.dat exist !"
		 read -p "Whether to start unpacking ? [y/n]: " var1
		 if [ "$var1" == "y" ];then
	 	 echo "system.dat exist !"
	 	 echo "Unpacking System.img..."
	 	 python3 ./tools/sdat2img.py $pj/system.transfer.list $pj/system.new.dat $pj/system_.img
	 	 echo "Conversion complete !"
	 	 read -p "Press any key continue..."
		 fi
	fi

	if [ ! -e ./$pj/system_ext.new.dat ];then
      if [ -f ./$pj/system_ext.new.dat.br ];then
    	 read -p "system_ext.new.dat.br exist, Whether to convert ? [y/n]: " var1
    	 if [ "$var1" == "y" ];then
    	 brotli -d ./$pj/system_ext.new.dat.br
    	 echo "system_ext.br==>dat done!"
	 	 echo "Unpacking System_ext.img..."
    	 python3 ./tools/sdat2img.py $pj/system_ext.transfer.list $pj/system_ext.new.dat $pj/system_ext_.img
    	 echo "Conversion complete !"
    	 read -p "Press any key continue..."
    	 fi
      fi
	else
		 echo "system_ext.new.dat exist !"
		 read -p "Whether to start unpacking ? [y/n]: " var1
		 if [ "$var1" == "y" ];then
	 	 echo "system_ext.dat exist !"
	 	 echo "Unpacking System.img..."
		 python3 ./tools/sdat2img.py $pj/system_ext.transfer.list $pj/system_ext.new.dat $pj/system_ext_.img
	 	 echo "Conversion complete !"
	 	 read -p "Press any key continue..."
		 fi
	fi

    if [ ! -e ./$pj/vendor.new.dat ];then
       if [ -f ./$pj/vendor.new.dat.br ];then
    	 read -p "vendor.new.dat.br exist, Whether to convert ? [y/n]: " var1
    	 if [ "$var1" == "y" ];then
    	 brotli -d ./$pj/vendor.new.dat.br
    	 echo "vendor.br==>dat done!"
	     echo "Unpacking vendor.img..."
    	 python3 ./tools/sdat2img.py $pj/vendor.transfer.list $pj/vendor.new.dat $pj/vendor_.img
    	 echo "Conversion complete !"
    	 read -p "Press any key continue..."
         fi
       fi
	else
		 echo "vendor.new.dat exist !"
		 read -p "Whether to start unpacking ? [y/n]: " var1
		 if [ "$var1" == "y" ];then
	   	 echo "vendor.new.dat exist !"
	 	 echo "Unpacking vendor.img..."
    	 python3 ./tools/sdat2img.py $pj/vendor.transfer.list $pj/vendor.new.dat $pj/vendor_.img
	 	 echo "Conversion complete !"
	 	 read -p "Press any key continue..."
		 fi
	fi

    if [ ! -e ./$pj/product.new.dat ];then
	   if [ -f ./$pj/product.new.dat.br ];then
    	 read -p "product.new.dat.br exist, Whether to convert ? [y/n]: " var1
    	 if [ "$var1" == "y" ];then
    	 brotli -d ./$pj/product.new.dat.br
    	 echo "product.br==>dat done!"
	     echo "Unpacking product.img..."
    	 python3 ./tools/sdat2img.py $pj/product.transfer.list $pj/product.new.dat $pj/product_.img
    	 echo "Conversion complete !"
    	 read -p "Press any key continue..."
    	 fi
       fi
	else
		 echo "product.new.dat exist !"
		 read -p "Whether to start unpacking ? [y/n]: " var1
		 if [ "$var1" == "y" ];then
		 echo "product.new.dat exist !"
	 	 echo "Unpacking product.img..."
    	 python3 ./tools/sdat2img.py $pj/product.transfer.list $pj/product.new.dat $pj/product_.img
	 	 echo "Conversion complete !"
	 	 read -p "Press any key continue..."
		 fi
	fi

	if [ ! -e ./$pj/odm.new.dat ];then
       if [ -f ./$pj/odm.new.dat.br ];then
    	 read -p "odm.new.dat.br exist, Whether to convert ? [y/n]: " var1
    	 if [ "$var1" == "y" ];then
    	 brotli -d ./$pj/odm.new.dat.br
    	 echo "odm.br==>dat done!"
		 echo "Unpacking odm.img..."
    	 python3 ./tools/sdat2img.py $pj/odm.transfer.list $pj/odm.new.dat $pj/odm_.img
    	 echo "Conversion complete !"
    	 read -p "Press any key continue..."
    	 fi
       fi
	else
		 echo "odm.new.dat exist !"
		 read -p "Whether to start unpacking ? [y/n]: " var1
		 if [ "$var1" == "y" ];then
		 echo "odm.new.dat exist !"
	 	 echo "Unpacking odm.img..."
    	 python3 ./tools/sdat2img.py $pj/odm.transfer.list $pj/odm.new.dat $pj/odm_.img
	 	 echo "Conversion complete !"
	 	 read -p "Press any key continue..."
		 fi
	fi

    if [ ! -e ./$pj/*.dat ];then
         echo "No anything dat file exist ! Please check directory ."
   	 	 read -p "Press any key return Main Menu..."
    fi
fi

if [ "$input_var" == 3 ];then  #TODO
   if [ -e ./$pj/system_.img ];then
   sudo make_ext4fs -T 0 -S file_contexts -l 2200M -a system system_new.img /system
   fi
   if [ -e ./$pj/system_ext_.img ];then
      sudo make_ext4fs -T 0 -S file_contexts -l 197M -a system_ext system_ext_new.img /system_ext
   fi
   if [ -e ./$pj/vendor_.img ];then
      sudo make_ext4fs -T 0 -S file_contexts -l 1100M -a vendor vendor_new.img /vendor
   fi
   if [ -e ./$pj/product_.img ];then
      sudo make_ext4fs -T 0 -S file_contexts -l 162M -a product product_new.img /product
   fi
   if [ -e ./$pj/odm_.img ];then
      sudo make_ext4fs -T 0 -S file_contexts -l 1M -a odm odm_new.img /odm
   fi
fi

if [ "$input_var" == 4 ];then
    echo "NOTE: Make sure that the repackaged .img file is in the Rom project directory." 
    read -p "press any key to continue..."
    if [ -e ./$pj/system_new.img ];then
        echo "Please Loading... Converting System.img to .dat file...  "
    	./tools/simg2img ./$pj/system_new.img -p system_new_.img
        python3 ./tools/rimg2sdat.py ./$pj/system_new_.img
        if [ "$?" == 0 ];then
        echo "Conversion is successful, start to convert to .br file "
        brotli -0 ./$pj/system.new_.dat
        echo "system.new.dat===>.br done. Outdir==$pj/"
        else echo "Conversion system.new.dat failed !"
        fi
    fi
    if [ -e ./$pj/system_ext_new.img ];then
        echo "Please Loading... Converting System_ext.img to .dat file...  "
        ./tools/simg2img ./$pj/system_ext_new.img -p system_ext_new_.img
        python3 ./tools/rimg2sdat.py ./$pj/system_ext_new_.img
        if [ "$?" == 0 ];then
        echo "Conversion is successful, start to convert to .br file "
        brotli -0 ./$pj/system_ext.new_.dat
        echo "system_ext_new.dat===>.br done. Outdir==$pj/"
        else echo "Conversion system_ext_new.dat failed !"
        fi
    fi
    if [ -e ./$pj/vendor_new.img ];then
        echo "Please Loading... Converting vendor.img to .dat file...  "
        ./tools/simg2img ./$pj/vendor_new.img -p vendor_new_.img
        python3 ./tools/rimg2sdat.py ./$pj/vendor_new_.img
        if [ "$?" == 0 ];then
        echo "Conversion is successful, start to convert to .br file "
        brotli -0 ./$pj/vendor.new_.dat
        echo "vendor_new.dat===>.br done. Outdir==$pj/"
        else echo "Conversion vendor_new.dat failed !"
        fi
    fi
    if [ -e ./$pj/product_new.img ];then
        echo "Please Loading... Converting product.img to .dat file...  "
        ./tools/simg2img ./$pj/product_new.img -p product_new_.img
        python3 ./tools/rimg2sdat.py ./$pj/product_new_.img
        if [ "$?" == 0 ];then
        echo "Conversion is successful, start to convert to .br file "
        brotli -0 ./$pj/product.new_.dat
        echo "product.new.dat===>.br done. Outdir==$pj/"
        else echo "Conversion product.new.dat failed !"
        fi
    fi
    if [ -e ./$pj/odm_new.img ];then
        echo "Please Loading... Converting odm.img to .dat file...  "
        ./tools/simg2img ./$pj/odm_new.img -p odm_new_.img
        python3 ./tools/rimg2sdat.py ./$pj/odm_new_.img
        if [ "$?" == 0 ];then
        echo "Conversion is successful, start to convert to .br file "
        brotli -0 ./$pj/odm.new_.dat
        echo "odm.new.dat===>.br done. Outdir==$pj/"
        else echo "Conversion odm.new.dat failed !"
        fi
    fi
    if [ ! -e ./$pj/*_new.img ];then
	echo -e "\nNo anything dat file exist ! Please check directory ."
    read -p "Press any key return Main Menu..." 
    fi
fi

if [ "$input_var" == 5 ];then
    while true
    do
    printf "\033c"   #Clean Screen
    echo -e "======================="
    echo -e "=                     ="
    echo -e "=    1.unpack boot    ="
    echo -e "=    2.repack boot    ="
    echo -e "=                     ="
    echo -e "======================="
    echo -e "Note: Please put the boot.img file under [ AIK-Linux ] directory."
    read -p "Please input number: " var
    if [ "$var" == 1 ];then
    	if [ -e ./AIK-Linux/boot.img ];then
    	echo "unpack boot..."
    	./AIK-Linux/unpackimg.sh ./boot.img
    	echo "unpack done..."
    	read -p "Press any key continue..."
    	else 
    	echo "No Such boot.img file !" 
    	echo "Please put the boot.img file under [ AIK-Linux ] directory."
    	read -p "Press any key continue..."
    	fi
    fi
    if [ "$var" == 2 ];then
    	echo "repack boot..."
    	./AIK-Linux/repackimg.sh
    	mv ./AIK-Linux/image-new.img ./
    	mv ./image-new.img new_boot.img
    	echo "repack done..."
    	echo "outdir===========>R_kitchen/"
    	read -p "Press any key return Main Menu..."
    	break
    fi
    done
fi


if [ "$input_var" == 6 ];then
	echo "Flashing package is being generated..."
	if [ -e "./$pj/system.new_.dat.br" ];then
		mv ./$pj/system.new_.dat.br ./$pj/Latest_Rom/
		mv ./$pj/Latest_Rom/system.new_.dat.br mv ./$pj/Latest_Rom/system.new.dat.br
		else
		mv ./$pj/system.new.dat.br ./$pj/Latest_Rom/
	fi

	if [ -e "./$pj/system_ext.new_.dat.br" ];then
		mv ./$pj/system_ext.new_.dat.br ./$pj/Latest_Rom/
		mv ./$pj/Latest_Rom/system_ext.new_.dat.br mv ./$pj/Latest_Rom/system_ext.new.dat.br
		else
		mv ./$pj/system_ext.new.dat.br ./$pj/Latest_Rom/
	fi

	if [ -e "./$pj/vendor.new_.dat.br" ];then
		mv ./$pj/vendor.new_.dat.br ./$pj/Latest_Rom/
		mv ./$pj/Latest_Rom/vendor.new_.dat.br mv ./$pj/Latest_Rom/vendor.new.dat.br
		else
		mv ./$pj/vendor.new.dat.br ./$pj/Latest_Rom/
	fi

	if [ -e "./$pj/product.new_.dat.br" ];then
		mv ./$pj/product.new_.dat.br ./$pj/Latest_Rom/
		mv ./$pj/Latest_Rom/product.new_.dat.br mv ./$pj/Latest_Rom/product.new.dat.br
		else
		mv ./$pj/product.new.dat.br ./$pj/Latest_Rom/
	fi

	if [ -e "./$pj/odm.new_.dat.br" ];then
		mv ./$pj/odm.new_.dat.br ./$pj/Latest_Rom/
		mv ./$pj/Latest_Rom/odm.new_.dat.br mv ./$pj/Latest_Rom/odm.new.dat.br
		else
		mv ./$pj/odm.new.dat.br ./$pj/Latest_Rom/
	fi

    zip -r ./$pj/"Rong"_${Rom_name%.*}_$Date.zip ./$pj/Latest_Rom/* > generate_rom.log
    if [ "$?" == 0 ];then
        echo "done !!!, Happy flashing !"
        read -p "Press any key return Main Menu ."
       else
		echo "Failed to generate, please check the error . Check generate_rom.log"
	    read -p "Press any key return Main Menu ."
	fi
fi

if [ "$input_var" == 0 ];then
	read -p "Note: Are you sure delete Project??? [y/n]" del
	if [ "$del" == "y" ];then  
    	     echo "Cleaning up..."
    	     rm -rf ./$pj
    	     echo "done"
    	     read -p "Press any key return Main Menu..."
    	fi
fi

if [ "$input_var" == "Q" ] || [ "$input_var" == "q" ];then 
	exit
fi
done

